package com.mhs.pb.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.mhs.pb.dao.DAOFactory;
import com.mhs.pb.dao.IDAO;
import com.mhs.pb.domain.Account;
import com.mhs.pb.domain.Basicinfo;
import com.mhs.pb.domain.Record;
import com.mhs.pb.domain.Relation;
import com.mhs.pb.domain.Resource;
import com.mhs.pb.service.IRelationManager;

public class RelationManager implements IRelationManager {
private IDAO<Relation> relationDao = DAOFactory.getDAO(Relation.class);
private IDAO<Basicinfo> basicInfoDao = DAOFactory.getDAO(Basicinfo.class);
private IDAO<Resource> resourceDao = DAOFactory.getDAO(Resource.class);
private IDAO<Record> recordDao = DAOFactory.getDAO(Record.class);

@Override
public List<Relation> listAllRelations(Account account) {
	List<Relation> accountRelations = relationDao.getBy(Relation.class, "relatedaccount", account.getId());
	return accountRelations;
}

@Override
public void addRelation(Relation relation) {
	Basicinfo basicinfo = relation.getBasicinfo();

	basicInfoDao.save(basicinfo);
	relationDao.save(relation);
}

@Override
public void modifyRelation(Relation relation) {
	Basicinfo basicinfo = relation.getBasicinfo();
	basicInfoDao.update(basicinfo);
	relationDao.update(relation);
}

@Override
public void deleteRelation(Relation relation) {
	Basicinfo info = new Basicinfo();
	info.setId(relation.getBasicinfo().getId());
	Set<Record> records = relation.getRecords();
	Set<Resource> resources = relation.getResources();
	for (Resource resource : resources) {
		resourceDao.del(resource);
	}
	for (Record record : records) {
		recordDao.del(record);
	}
	
	relationDao.del(relation);
	basicInfoDao.del(info);
}

@Override
public void deleteRecord(Record record) {
	recordDao.del(record);
}

@Override
public void deleteResource(Resource resource) {
	resourceDao.del(resource);
}

@Override
public List<Relation> getRelationsByName(Account account, String name) {
	String queryStr = "select distinct re from Relation as re inner join re.basicinfo as bi where re.relatedaccount = ? and (bi.name like concat('%','?','%') or bi.nickname like concat('%','?','%') or bi.usednames like concat('%','?','%'))";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), name, name, name }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByCellPhone(Account account, String cellphone) {
	String queryStr = "select distinct re from Relation as re inner join re.basicinfo as bi where re.relatedaccount = ? and bi.cellphones like concat('%','?','%')";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), cellphone }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByPhone(Account account, String phone) {
	String queryStr = "select distinct re from Relation as re inner join re.basicinfo as bi where re.relatedaccount = ? and bi.phones like concat('%','?','%')";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), phone }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByContact(Account account, String contact) {
	String queryStr = "select distinct re from Relation as re inner join re.basicinfo as bi where re.relatedaccount = ? and bi.contacts like concat('%','?','%')";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), contact }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByFavors(Account account, String... favors) {
	String queryStr = "select distinct re from Relation as re inner join re.basicinfo as bi where re.relatedaccount = ? and bi.favors like concat('%','?','%')";
	StringBuilder builder = new StringBuilder();
	for (String favor : favors) {
		builder.append(favor);
		builder.append("%");
	}
	if (builder.length() > 0) {
		builder.deleteCharAt(builder.length() - 1);
	}
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), builder.toString() }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByGoodAts(Account account, String... goodAts) {
	String queryStr = "select distinct re from Relation as re inner join re.basicinfo as bi where re.relatedaccount = ? and bi.goodat like concat('%','?','%') ";
	StringBuilder builder = new StringBuilder();
	for (String goodat : goodAts) {
		builder.append(goodat);
		builder.append("%");
	}
	if (builder.length() > 0) {
		builder.deleteCharAt(builder.length() - 1);
	}
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), builder.toString() }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByAccomplishment(Account account, String accomplishment) {
	String queryStr = "select distinct re from Relation as re inner join re.records as rc where re.relatedaccount=? and rc.accomplishment like concat('%', '?', '%')";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), accomplishment }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByTypeAndAccomplishment(Account account, String type, String accomplishment) {
	String queryStr = "select distinct re from Relation as re inner join re.records as rc where re.relatedaccount=? and rc.type=? and rc.accomplishment like concat('%', '?', '%')";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), type, accomplishment }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByPersonalRecordDes(Account account, String recordDes) {
	String queryStr = "select distinct re from Relation as re inner join re.records as rc where re.relatedaccount=? and rc.description like concat('%', '?', '%')";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), recordDes }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByTypeAndPersonalRecordDes(Account account, String type, String recordDes) {
	String queryStr = "select distinct re from Relation as re inner join re.records as rc where re.relatedaccount=? and rc.type=? and rc.description like concat('%', '?', '%')";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), type, recordDes }));
	return relationsByName;
}

@Override
public List<Relation> getRelationsByResourceDes(Account account, String resourceDes) {
	String queryStr = "select distinct re from Relation as re inner join re.resources as rc where re.relatedaccount=? and rc.description like concat('%', '?', '%')";
	List<Relation> relationsByName = relationDao.query(Relation.class, queryStr, Arrays.asList(new Object[] { account.getId(), resourceDes }));
	return relationsByName;
}

}
